package com.xiyi.shouxin.dao.user;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Repository;

import com.xiyi.shouxin.dao.BaseRepository;
import com.xiyi.shouxin.dao.JdbcPreparedStatement;
import com.xiyi.shouxin.entity.user.UserAnswer;
import com.xiyi.shouxin.entity.user.mapper.UserAnswerMapper;
import com.xiyi.shouxin.exception.CommonException;
import com.xiyi.shouxin.exception.ErrorCode;

@Repository
public class UserAnswerRepository extends BaseRepository<UserAnswer> {

    public UserAnswerRepository() {
        super();
        super.stat = new JdbcPreparedStatement<>(UserAnswer.class);
    }

    public UserAnswer findFirstByUid(Long uid) throws CommonException {
        if (uid == null) {
            throw new CommonException(ErrorCode.INVALIDPARAMTER);
        }
        StringBuilder sb = new StringBuilder("SELECT ");
        sb.append(stat.GetQueryColumns());
        sb.append(" FROM ");
        sb.append(stat.GetTableName());
        sb.append(" WHERE ");
        sb.append(stat.GetColumnName("uid"));
        sb.append(" = ? LIMIT 1");
        try {
            UserAnswer answer = template.queryForObject(sb.toString(), new Object[] { uid }, new UserAnswerMapper());
            return answer;
        } catch (EmptyResultDataAccessException e) {

        }
        return null;
    }

}
